package com.lucun.insurance.service.Impl;

import com.lucun.insurance.db.entity.Insure;
import com.lucun.insurance.db.entity.Settlement;
import com.lucun.insurance.db.mapper.InsureMapper;
import com.lucun.insurance.db.mapper.SettlementMapper;
import com.lucun.insurance.service.SettlementService;
import com.lucun.insurance.util.BaseException;
import com.lucun.insurance.util.JsonResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class SettlementServiceImpl implements SettlementService {
    @Autowired
    SettlementMapper settlementMapper;
    //录入理赔信息
    @Override
    public JsonResult<?> insert(Settlement sett) {

        if (ObjectUtils.isEmpty(sett)){
            throw  new BaseException("录入数据不能为空！");
        }
        String name = sett.getRequestName();
        if (StringUtils.isBlank(name)) {
            throw new BaseException("申请人姓名不能为空！");
        }
        String category = sett.getCategory();
        if (ObjectUtils.isEmpty(category)) {
            throw new BaseException("费用类别不能为空！");
        }
        String hos =sett.getHospitalCategory();
        if (ObjectUtils.isEmpty(hos)) {
            throw new BaseException("医院类别不能为空！");
        }
        String hospitalName =sett.getHospitalName();
        if (ObjectUtils.isEmpty(hospitalName)) {
            throw new BaseException("医院名称不能为空！");
        }
        sett.setRequestTime(new Date());
//        Date dangerTime = sett.getDangerTime();
//        if (ObjectUtils.isEmpty(dangerTime)) {
//            throw new BaseException("出险时间不能为空！");
//        }
        Float amount = sett.getAmount();
        if (ObjectUtils.isEmpty(amount)) {
            throw new BaseException("申请金额不能为空！");
        }
        String  accident =sett.getAccident();
        if (ObjectUtils.isEmpty(accident)) {
            throw new BaseException("出险原因不能为空！");
        }
        String disease =sett.getDisease();
        if (ObjectUtils.isEmpty(disease)) {
            throw new BaseException("疾病名称不能为空！");
        }
        String bank = sett.getBank();
        if (ObjectUtils.isEmpty(bank)) {
            throw new BaseException("开户行不能为空！");
        }
        String bankAccount = sett.getBankAccount();
        if (ObjectUtils.isEmpty(bankAccount)) {
            throw new BaseException("银行账户不能为空！");
        }
//        if (bankAccount<15||bankAccount>19){
//            throw new BaseException("银行账户输入有误！");
//
//        }
        sett.setCreationTime(new Date());
        int i=settlementMapper.insert(sett);
        if (i != 0) {
            return new JsonResult<>("200","信息录入成功!",sett);
        }
        return new JsonResult<>("500","信息录入失败！");
    }
//理赔信息查询（全部）
    @Override
    public List<Settlement> findAll() {
        return settlementMapper.findAll();
    }
//根据ID查询理赔信息
    @Override
    public List<Settlement> findById(Settlement id) {

        return settlementMapper.findById(id);
    }
//根据投保人（申请人）姓名查询理赔信息
    @Override
    public JsonResult<?> findByName(Settlement settlement) {
        List<Settlement> i=settlementMapper.findByName(settlement);
        if (i != null) {
            return new  JsonResult("200","信息查询成功!",i);
        }
        return new JsonResult<>("500","信息查询失败,无此人信息！");
    }
    //根据id删除
    @Override
    public JsonResult<?> delete(Settlement sett) {
        int id=sett.getId();
        if (ObjectUtils.isEmpty(id)){
            throw new BaseException("没有获取此id");
        }
        int i = settlementMapper.deleteByPrimaryKey(id);
        if (i != 0) {
            return new JsonResult<>("200","删除成功");
        }
        return new JsonResult<>("500","删除失败");
    }

    @Override
    public JsonResult<?> update(Settlement sett) {
        sett.setEditTime(new Date());
        if (ObjectUtils.isEmpty(sett.getEditTime())) {
            throw new BaseException("编辑时间不能为空！");
        }
        int i = settlementMapper.updateByPrimaryKeySelective(sett);
        if (i!=0){
            return new JsonResult<>("200","信息编辑成功",sett);
        }
        return  new JsonResult<>("500");
    }
}
